package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// UserAddress 用户收货地址
type UserAddress struct {
	ID            string `json:"id" gorm:"primaryKey;column:id;type:bigint;comment:唯一ID"`
	OpenID        string `json:"openid,omitempty" gorm:"column:openid;type:string;default:NULL;size:50"`
	Default       string `json:"default" gorm:"column:default;type:int;size:8;comment:1-默认地址 2-否"`
	UserName      string `json:"user_name" gorm:"column:user_name;type:string;size:30;comment:收件人姓名"`
	TelNumber     string `json:"tel_number" gorm:"column:tel_number;type:string;size:20;comment:收货人手机号"`
	PostalCode    string `json:"postal_code" gorm:"column:postal_code;type:string;size:255;comment:邮编"`
	ProvinceName  string `json:"province_name" gorm:"column:province_name;type:string;size:10;comment:国标收货地址第一级地址"`
	CityName      string `json:"city_name" gorm:"column:city_name;type:string;size:10;comment:国标收货地址第二级地址"`
	CountyName    string `json:"county_name" gorm:"column:county_name;type:string;size:20;comment:国标收货地址第三级地址"`
	StreetName    string `json:"street_name" gorm:"column:street_name;type:string;size:20;comment:国标收货地址第四级地址"`
	DetailAddress string `json:"detail_address" gorm:"column:detail_address;type:string;size:100;comment:详细地址"`
}

func (UserAddress) TableName() string {
	return "user_address"
}

// BeforeCreate 自增id
func (m *UserAddress) BeforeCreate(tx *gorm.DB) (err error) {
	if m.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	m.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &UserAddress{})
}
